﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KhoBanHang
{
    public partial class PhieuGiaoHang_DanhSach : Form
    {
        
        List<Business.PhieuGiaoHang> danhSachPhieuGiaoHang;
        

        
        public PhieuGiaoHang_DanhSach()
        {
            InitializeComponent();
        }

        private void PhieuGiaoHang_DanhSach_Load(object sender, EventArgs e)
        {
            dtpickerTuNgay.CustomFormat = "dd/MM/yyyy";
            dtpickerDenNgay.CustomFormat = "dd/MM/yyyy";
            LoadDanhsachPhieuGiaoHang(true);
        }

        private void LoadDanhsachPhieuGiaoHang(bool lanDau)
        {
            if (lanDau)
            {
                danhSachPhieuGiaoHang = Business.PhieuGiaoHang.LayDanhSach();
            }
            else

            {
                if (rbtnNgayXuat.Checked && dtpickerTuNgay.Value != null)
                    danhSachPhieuGiaoHang = Business.PhieuGiaoHang.LayDanhSach_NgayGiao(dtpickerTuNgay.Value, dtpickerDenNgay.Value);
                else if (rbtnMaHD.Checked && txtMaHD.Text.Contains("HDB"))
                    danhSachPhieuGiaoHang = Business.PhieuGiaoHang.LayDanhSach_MaHoaDonBan(txtMaHD.Text);
                else if (rbtnMaPhieuGiao.Checked && txtMaPhieuGiao.Text.Contains("PGH"))
                    danhSachPhieuGiaoHang = Business.PhieuGiaoHang.LayDanhSach_MaPhieuGiao(txtMaPhieuGiao.Text);
                else
                    danhSachPhieuGiaoHang = Business.PhieuGiaoHang.LayDanhSach();

            }

            gridviewDSPhieuGH.DataSource = danhSachPhieuGiaoHang;

            gridviewDSPhieuGH.Columns["MaPGH"].Visible = false;
            gridviewDSPhieuGH.Columns["MaHDB"].Visible = false;

            gridviewDSPhieuGH.Columns["MaNV"].Visible = false;

            gridviewDSPhieuGH.Columns["DiaChiKhachHang"].Visible = false;
            gridviewDSPhieuGH.Columns["DienThoai"].Visible = false;
            gridviewDSPhieuGH.Columns["MaSoThue"].Visible = false;

            gridviewDSPhieuGH.Columns["TienTruocThue"].Visible = false;
            gridviewDSPhieuGH.Columns["ThueSuat"].Visible = false;
            gridviewDSPhieuGH.Columns["ThueSuatHienThi"].Visible = false;

            gridviewDSPhieuGH.Columns["NgayGiao"].DefaultCellStyle.Format = "dd/MM/yyyy";

            gridviewDSPhieuGH.Columns["MaPGHHienThi"].HeaderText = "Mã phiếu giao hàng";
            gridviewDSPhieuGH.Columns["MaNVHienThi"].HeaderText = "Nhân viên lập";
            gridviewDSPhieuGH.Columns["MaHDBHienThi"].HeaderText = "Hóa đơn bán";

            gridviewDSPhieuGH.Columns["NgayGiao"].HeaderText = "Ngày giao hàng";
            gridviewDSPhieuGH.Columns["TenKhachHang"].HeaderText = "Tên khách hàng";
            gridviewDSPhieuGH.Columns["TienSauThue"].HeaderText = "Tổng tiền";
        }
        

      
        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDanhsachPhieuGiaoHang(false);
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (danhSachPhieuGiaoHang != null && danhSachPhieuGiaoHang.Count > 0)
            {
                if (gridviewDSPhieuGH.SelectedRows.Count == 0)
                {
                    int maPGH = danhSachPhieuGiaoHang[0].MaPGH;

                    LoadPhieuGiaoHang(maPGH);
                }
                else
                {
                    int maPGH = danhSachPhieuGiaoHang[gridviewDSPhieuGH.SelectedRows[0].Index].MaPGH;

                    LoadPhieuGiaoHang(maPGH);
                }
            }

        }

        private void LoadPhieuGiaoHang(int maPGH)
        {
            PhieuGiaoHang form = new PhieuGiaoHang();
            form.LayTheoPGH(maPGH);

            form.ShowDialog();

        }
        

        private void btnXem_Click_1(object sender, EventArgs e)
        {
            if (danhSachPhieuGiaoHang != null && danhSachPhieuGiaoHang.Count > 0)
            {
                if (gridviewDSPhieuGH.SelectedRows.Count == 0)
                {
                    int maPGH = danhSachPhieuGiaoHang[0].MaPGH;

                    LoadPhieuGiaoHang(maPGH);
                }
                else
                {
                    int maPGH = danhSachPhieuGiaoHang[gridviewDSPhieuGH.SelectedRows[0].Index].MaPGH;

                    LoadPhieuGiaoHang(maPGH);
                }
            }
        }

        private void btnTim_Click_1(object sender, EventArgs e)
        {
            LoadDanhsachPhieuGiaoHang(false);
        }
    }
}
